<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;

//linux系统探测
function sys_linux()
{
    // CPU
    if (false === ($str = @file("/proc/cpuinfo"))) return false;

    $str = implode("", $str);
    // UPTIME
    if (false === ($str = @file("/proc/uptime"))) return false;
    $str = explode(" ", implode("", $str));
    $str = trim($str[0]);
    $min = $str / 60;
    $hours = $min / 60;
    $days = floor($hours / 24);
    $hours = floor($hours - ($days * 24));
    $min = floor($min - ($days * 60 * 24) - ($hours * 60));
    if ($days !== 0) $res['uptime'] = $days."天";
    if ($hours !== 0) $res['uptime'] .= $hours."小时";
    $res['uptime'] .= $min."分钟";

    // MEMORY
    if (false === ($str = @file("/proc/meminfo"))) return false;
    $str = implode("", $str);
    preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);

    $res['memTotal'] = round($buf[1][0]/1024, 2);
    $res['memFree'] = round($buf[2][0]/1024, 2);
	$res['memCached'] = round($buf[3][0]/1024, 2);
    $res['memUsed'] = $res['memTotal']-$res['memFree'];
    $res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memUsed']/$res['memTotal']*100,2):0;

    // LOAD AVG
    if (false === ($str = @file("/proc/loadavg"))) return false;
    $str = explode(" ", implode("", $str));
    $str = array_chunk($str, 4);
    $res['loadAvg'] = implode(" ", $str[0]);

    return $res;
}

class AdminController extends Controller
{	
	public $module = 'dashboard';

	public $page_name = 'imlooke 管理面板';

    public $page_describe = 'Version 1.0';

    public function index()
    {
    	// 系统时间
        $stime = date("Y-n-j H:i:s");
        // 根据不同系统取得CPU相关信息
		$sysInfo = sys_linux();

		// 系统时间
		$stime = date("Y-n-j H:i:s");
		// 系统运行时间
		$uptime = $sysInfo['uptime'];
		$df = round(@disk_free_space(".")/(1024*1024*1024),3);
		$dt = round(@disk_total_space(".")/(1024*1024*1024),3);
		$memTotal = round($sysInfo['memTotal']/1024,3)." GB";
		$mu = round($sysInfo['memUsed']/1024,3)." GB";
		$mf = round($sysInfo['memFree']/1024,3)." GB";
		$memPercent = $sysInfo['memPercent']; //内存总使用率
		//网卡流量
		$strs = @file("/proc/net/dev"); 

		for ($i = 2; $i < count($strs); $i++ )
		{
			preg_match_all( "/([^\s]+):[\s]{0,}(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/", $strs[$i], $info );
			$tmo = round($info[2][0]/1024/1024, 5); 
			$tmo2 = round($tmo / 1024, 5);
			$NetInput[$i] = $tmo2;
			$tmp = round($info[10][0]/1024/1024, 5); 
			$tmp2 = round($tmp / 1024, 5);
			$NetOut[$i] = $tmp2;
		}

		// 数据统计
		$userCount = \App\Models\API\User::count();
		$activityCount = \App\Models\API\Activity::count();
		$tweetCount = \App\Models\API\Tweet::count();
		$feedbackCount = \App\Models\Admin\Feedback::count();
        $arr = [
        	'sysInfo',
        	'stime','uptime',
        	'df','dt',
        	'memTotal','mu','mf','memPercent',
        	'NetInput','NetOut',
        	'userCount','activityCount','tweetCount','feedbackCount',
        ];

        return view('index' ,compact($arr));
    }

    public function getJson()
    {
        // 系统时间
        $stime = date("Y-n-j H:i:s");
        // 根据不同系统取得CPU相关信息
		$sysInfo = sys_linux();
		// 系统时间
		$stime = date("Y-n-j H:i:s");
		// 系统运行时间
		$uptime = $sysInfo['uptime'];
		$memPercent = $sysInfo['memPercent']; //内存总使用率
        $arr = [
        	'stime' => $stime,'uptime' => $uptime,
        	'memPercent' => $memPercent,
        ];

        return response()->json($arr);
    }
}
